<?php 
Yii::import('ext\phpThumb\ThumbLib.inc', true);
class Image extends PhpThumbFactory{
	
	public static function create ($filename = null, $options = array(), $isDataStream = false)
	{
		// map our implementation to their class names
		$implementationMap = array
		(
				'imagick'	=> 'ImagickThumb',
				'gd' 		=> 'GdThumber'
		);
	
		// grab an instance of PhpThumb
		$pt = PhpThumb::getInstance();
		// load the plugins
		$pt->loadPlugins(self::$pluginPath);
	
		$toReturn = null;
		$implementation = self::$defaultImplemenation;
	
		// attempt to load the default implementation
		if ($pt->isValidImplementation(self::$defaultImplemenation))
		{
			$imp = $implementationMap[self::$defaultImplemenation];
			$toReturn = new $imp($filename, $options, $isDataStream);
		}
		// load the gd implementation if default failed
		else if ($pt->isValidImplementation('gd'))
		{
			$imp = $implementationMap['gd'];
			$implementation = 'gd';
			$toReturn = new $imp($filename, $options, $isDataStream);
		}
		// throw an exception if we can't load
		else
		{
			throw new Exception('You must have either the GD or iMagick extension loaded to use this library');
		}
	
		$registry = $pt->getPluginRegistry($implementation);
		$toReturn->importPlugins($registry);
		return $toReturn;
	}
}

class GdThumber extends GdThumb{
	
// 	public function save ($fileName, $format = null){
// 		//gd库imagepng/imagejpg/imagegif 不支持stream保存文件 5.4以上才支持
// 		if((version_compare(phpversion(), "5.4") > 0) || (strpos($fileName, '://') === false))
// 			return parent::save($fileName, $format = null);
		
// 		$validFormats = array('GIF', 'JPG', 'PNG');
// 		$format = ($format !== null) ? strtoupper($format) : $this->format;
		
// 		if (!in_array($format, $validFormats))
// 		{
// 			throw new InvalidArgumentException ('Invalid format type specified in save function: ' . $format);
// 		}
		
// 		// make sure the directory is writeable
// 		if (!is_writeable(dirname($fileName)))
// 		{
// 			// try to correct the permissions
// 			if ($this->options['correctPermissions'] === true)
// 			{
// 				@chmod(dirname($fileName), 0777);
				
// 				// throw an exception if not writeable
// 				if (!is_writeable(dirname($fileName)))
// 				{
// 					throw new RuntimeException ('File is not writeable, and could not correct permissions: ' . $fileName);
// 				}
// 			}
// 			// throw an exception if not writeable
// 			else
// 			{
// 				throw new RuntimeException ('File not writeable: ' . $fileName);
// 			}
// 		}
		
// 		// When the interlace option equals true or false call imageinterlace else leave it to default
// 		if ($this->options['interlace'] === true)
// 			imageinterlace($this->oldImage, 1);
// 		elseif ($this->options['interlace'] === false)
// 			imageinterlace($this->oldImage, 0);
		
// 		ob_start();
// 		switch ($format)
// 		{
// 			case 'GIF':
// 				imagegif($this->oldImage, null);
// 				break;
// 			case 'JPG':
// 				imagejpeg($this->oldImage, null, $this->options['jpegQuality']);
// 				break;
// 			case 'PNG':
// 				imagepng($this->oldImage, null);
// 				break;
// 		}
// 		$cotent = ob_get_contents();
// 		file_put_contents($fileName, $cotent);
// 		ob_end_clean();
// 		return $this;
// 	}
}
?>